﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace HospitalSystem.Core
{
    public class regfamilyLog

    {
        public void radioMariageStatus(RadioButtonList MariageStatus)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            //LiveStatus

            SqlCommand CmdSql2 = new SqlCommand("SELECT MariageStatus FROM [r_MariageStatus]", conn);
            conn.Open();



            DataTable dt = new DataTable();

            SqlDataAdapter sqlDa = new SqlDataAdapter();

            sqlDa.SelectCommand = CmdSql2;
            sqlDa.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                MariageStatus.DataSource = dt;
                MariageStatus.DataTextField = "MariageStatus"; //Where Anwsers is the column name from the database
                MariageStatus.DataValueField = "MariageStatus";
                MariageStatus.DataBind();
            }
            conn.Close();

        }








        public void radioLiveStatus(RadioButtonList LiveStatus)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand CmdSql22 = new SqlCommand("SELECT LiveStatus FROM [r_LiveStatus]", conn);
            conn.Open();

            DataTable dt2 = new DataTable();

            SqlDataAdapter sqlDa2 = new SqlDataAdapter();

            sqlDa2.SelectCommand = CmdSql22;
            sqlDa2.Fill(dt2);
            if (dt2.Rows.Count > 0)
            {
                LiveStatus.DataSource = dt2;
                LiveStatus.DataTextField = "LiveStatus"; //Where Anwsers is the column name from the database
                LiveStatus.DataValueField = "LiveStatus";
                LiveStatus.DataBind();
            }
            conn.Close();



        }











        // ดึงข้อมูล สถานะขึ้นให้ดู
        public void loaddataMariageStatus(RadioButtonList MariageStatus,Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT MariageStatus FROM [Family] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {

                MariageStatus.Text = reader2["MariageStatus"].ToString();

            }
            else
            {
                //MessageBox("ไม่พบเลขบัตรครับ");
            }
            conn.Close();
            reader2.Close();

        }





        public void loaddataCarer(Label cid_label, TextBox Name, TextBox LastName, TextBox CID, RadioButtonList LiveStatus, TextBox Race, TextBox Religion, Label fcid, TextBox LiveHouseNumber, TextBox LiveHouseName, TextBox LiveHouseMoo, TextBox LiveHousesoy, TextBox LiveStreetName, TextBox Livetambon, TextBox LiveCity, TextBox LiveProvince, TextBox LivePostcode, TextBox CensusHouseNumber, TextBox CensusHouseName, TextBox CensusHouseMoo, TextBox CensusHousesoy, TextBox CensusStreetName, TextBox Censustambon, TextBox CensusCity, TextBox CensusProvince, TextBox CensusPostcode, TextBox IncomePerMonth, TextBox AddressOccupation, TextBox OherIncome, TextBox OherIncomeFrom)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Name,LastName,fCID,LiveStatus,Nation,Race,Religion,LiveHouseNumber,LiveHouseName,LiveHouseMoo,LiveHousesoy,LiveStreetName,Livetambon,LiveCity,LiveProvince,LivePostcode,CensusHouseNumber,CensusHouseName,CensusHouseMoo,CensusHousesoy,CensusStreetName,Censustambon,CensusCity,CensusProvince,CensusPostcode,IncomePerMonth,EduLevel,AddressOccupation,Occupation,OherIncome,OherIncomeFrom FROM [Family] where CID=@CID and fCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);
            CmdSql2.Parameters.AddWithValue("@fCID", fcid.Text);
            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                Name.Text = reader2["Name"].ToString();
                LastName.Text = reader2["LastName"].ToString();
                CID.Text = reader2["fCID"].ToString();
                LiveStatus.Text = reader2["LiveStatus"].ToString();
                Race.Text = reader2["Race"].ToString();
                Religion.Text = reader2["Religion"].ToString(); 
                LiveHouseNumber.Text=reader2["LiveHouseNumber"].ToString();
                LiveHouseName.Text =reader2["LiveHouseName"].ToString();
                LiveHouseMoo.Text = reader2["LiveHouseMoo"].ToString();
                LiveHousesoy.Text = reader2["LiveHousesoy"].ToString();
                Livetambon.Text = reader2["Livetambon"].ToString();
                LiveCity.Text = reader2["LiveCity"].ToString();
                LiveProvince.Text = reader2["LiveProvince"].ToString();
                LivePostcode.Text = reader2["LivePostcode"].ToString();
                CensusHouseNumber.Text = reader2["CensusHouseNumber"].ToString();
                CensusHouseName.Text = reader2["CensusHouseName"].ToString();
                CensusHouseMoo.Text = reader2["CensusHouseMoo"].ToString();
                CensusHousesoy.Text = reader2["CensusHousesoy"].ToString();
                Censustambon.Text = reader2["Censustambon"].ToString();
                CensusCity.Text = reader2["CensusCity"].ToString();
                CensusProvince.Text = reader2["CensusProvince"].ToString();
                CensusPostcode.Text = reader2["CensusPostcode"].ToString();
                IncomePerMonth.Text = reader2["IncomePerMonth"].ToString();
                OherIncome.Text = reader2["OherIncome"].ToString();
                OherIncomeFrom.Text = reader2["OherIncomeFrom"].ToString();


            }
            else
            {
              
            }
            conn.Close();
            reader2.Close();

        }










        public void insertregdata(Label cid_label,RadioButtonList MariageStatus,String Name,String LastName,String CID,String Race,String Religion,RadioButtonList LiveStatus,String LiveHouseNumber,String LiveHouseName,String LiveHouseMoo,String LiveHousesoy,String LiveStreetName,String Livetambon,String LiveCity,String LiveProvince,String LivePostcode,String CensusHouseNumber,String CensusHouseName,String CensusHouseMoo,String CensusHousesoy,String CensusStreetName,String Censustambon,String CensusCity,String CensusProvince,String CensusPostcode,String IncomePerMonth,RadioButtonList EduLevel,String AddressOccupation,RadioButtonList Occupation,String OherIncome,String OherIncomeFrom)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);
            SqlCommand CmdSql2 = new SqlCommand("INSERT INTO [Family] (CID,MariageStatus,Name,LastName,fCID,Race,Religion,LiveStatus,LiveHouseNumber,LiveHouseName,LiveHouseMoo,LiveHousesoy,LiveStreetName,Livetambon,LiveCity,LiveProvince,LivePostcode,CensusHouseNumber,CensusHouseName,CensusHouseMoo,CensusHousesoy,CensusStreetName,Censustambon,CensusCity,CensusProvince,CensusPostcode,IncomePerMonth,EduLevel,AddressOccupation,Occupation,OherIncome,OherIncomeFrom) VALUES (@CID,@MariageStatus,@Name,@LastName,@fCID,@Race,@Religion,@LiveStatus,@LiveHouseNumber,@LiveHouseName,@LiveHouseMoo,@LiveHousesoy,@LiveStreetName,@Livetambon,@LiveCity,@LiveProvince,@LivePostcode,@CensusHouseNumber,@CensusHouseName,@CensusHouseMoo,@CensusHousesoy,@CensusStreetName,@Censustambon,@CensusCity,@CensusProvince,@CensusPostcode,@IncomePerMonth,@EduLevel,@AddressOccupation,@Occupation,@OherIncome,@OherIncomeFrom)", conn2);
            conn2.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);
            

            CmdSql2.Parameters.AddWithValue("@MariageStatus", MariageStatus.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@Name", Name);
            CmdSql2.Parameters.AddWithValue("@LastName", LastName);
            CmdSql2.Parameters.AddWithValue("@fCID", CID);
            //CmdSql2.Parameters.AddWithValue("@Nation", Nationd);
            CmdSql2.Parameters.AddWithValue("@Race", Race);
            CmdSql2.Parameters.AddWithValue("@Religion", Religion);
            CmdSql2.Parameters.AddWithValue("@LiveStatus", LiveStatus.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LiveHouseNumber", LiveHouseNumber);
            CmdSql2.Parameters.AddWithValue("@LiveHouseName", LiveHouseName);
            CmdSql2.Parameters.AddWithValue("@LiveHouseMoo", LiveHouseMoo);
            CmdSql2.Parameters.AddWithValue("@LiveHousesoy", LiveHousesoy);
            CmdSql2.Parameters.AddWithValue("@LiveStreetName",LiveStreetName);
            CmdSql2.Parameters.AddWithValue("@Livetambon", Livetambon);
            CmdSql2.Parameters.AddWithValue("@LiveCity",LiveCity);
            CmdSql2.Parameters.AddWithValue("@LiveProvince",LiveProvince);
            CmdSql2.Parameters.AddWithValue("@LivePostcode", LivePostcode);
            CmdSql2.Parameters.AddWithValue("@CensusHouseNumber", CensusHouseNumber);
            CmdSql2.Parameters.AddWithValue("@CensusHouseName",CensusHouseName);
            CmdSql2.Parameters.AddWithValue("@CensusHouseMoo", CensusHouseMoo);
            CmdSql2.Parameters.AddWithValue("@CensusHousesoy", CensusHousesoy);
            CmdSql2.Parameters.AddWithValue("@CensusStreetName", CensusStreetName);
            CmdSql2.Parameters.AddWithValue("@Censustambon", Censustambon);
            CmdSql2.Parameters.AddWithValue("@CensusCity", CensusCity);
            CmdSql2.Parameters.AddWithValue("@CensusProvince", CensusProvince);
            CmdSql2.Parameters.AddWithValue("@CensusPostcode", CensusPostcode);
            CmdSql2.Parameters.AddWithValue("@IncomePerMonth" , IncomePerMonth);
            CmdSql2.Parameters.AddWithValue("@EduLevel",EduLevel.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@AddressOccupation", AddressOccupation);
            CmdSql2.Parameters.AddWithValue("@Occupation", Occupation.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@OherIncome", OherIncome);
            CmdSql2.Parameters.AddWithValue("@OherIncomeFrom" ,OherIncomeFrom);
            CmdSql2.ExecuteNonQuery();
            conn2.Close();

        }




        public void showlistfamily(Label cid_label, DropDownList familylist)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select Name,LastName,fCID from Family where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", cid_label.Text);
            scon.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            familylist.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "<เลือกชื่อผู้ดูแลหลัก>";
            newItem.Value = "0";
            familylist.Items.Add(newItem);

            while (reader.Read() != false)
            {


                newItem = new ListItem();
                newItem.Text = reader["Name"].ToString() + ' ' + reader["LastName"].ToString();
                newItem.Value = reader["fCID"].ToString();
                familylist.Items.Add(newItem);



            }


            newItem.Text = "ผู้ดูแลหลักไม่ใช่คนในครอบครัว";
            newItem.Value = "0";

            reader.Close();
            scon.Close();
        }





        public void showlistfamily2(Label cid_label,GridView familylist)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select Name,LastName,fCID,MariageStatus from Family where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", cid_label.Text);
            scon.Open();
            SqlDataReader reader = cmd.ExecuteReader();



                familylist.DataSource = reader;
                familylist.DataBind();
                

            


            reader.Close();
         
        }









        public void insertregdataCarer(Label cid_label, String Name, String LastName, String CID, String Race, String Religion, RadioButtonList LiveStatus, String LiveHouseNumber, String LiveHouseName, String LiveHouseMoo, String LiveHousesoy, String LiveStreetName, String Livetambon, String LiveCity, String LiveProvince, String LivePostcode, String CensusHouseNumber, String CensusHouseName, String CensusHouseMoo, String CensusHousesoy, String CensusStreetName, String Censustambon, String CensusCity, String CensusProvince, String CensusPostcode, String IncomePerMonth, RadioButtonList EduLevel, String AddressOccupation, RadioButtonList Occupation, String OherIncome, String OherIncomeFrom)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;

                                    


            SqlConnection conn2 = new SqlConnection(constr);
            SqlCommand CmdSql = new SqlCommand("delete from [Carer] where CID=@CID", conn2);
            conn2.Open();
            CmdSql.Parameters.AddWithValue("@CID", cid_label.Text);

            CmdSql.ExecuteNonQuery();
            conn2.Close();


            
















         
            SqlCommand CmdSql2 = new SqlCommand("INSERT INTO [Carer] (CID,Name,LastName,fCID,Race,Religion,LiveStatus,LiveHouseNumber,LiveHouseName,LiveHouseMoo,LiveHousesoy,LiveStreetName,Livetambon,LiveCity,LiveProvince,LivePostcode,CensusHouseNumber,CensusHouseName,CensusHouseMoo,CensusHousesoy,CensusStreetName,Censustambon,CensusCity,CensusProvince,CensusPostcode,IncomePerMonth,EduLevel,AddressOccupation,Occupation,OherIncome,OherIncomeFrom) VALUES (@CID,@Name,@LastName,@fCID,@Race,@Religion,@LiveStatus,@LiveHouseNumber,@LiveHouseName,@LiveHouseMoo,@LiveHousesoy,@LiveStreetName,@Livetambon,@LiveCity,@LiveProvince,@LivePostcode,@CensusHouseNumber,@CensusHouseName,@CensusHouseMoo,@CensusHousesoy,@CensusStreetName,@Censustambon,@CensusCity,@CensusProvince,@CensusPostcode,@IncomePerMonth,@EduLevel,@AddressOccupation,@Occupation,@OherIncome,@OherIncomeFrom)", conn2);
            conn2.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);


            CmdSql2.Parameters.AddWithValue("@Name", Name);
            CmdSql2.Parameters.AddWithValue("@LastName", LastName);
            CmdSql2.Parameters.AddWithValue("@fCID", CID);
            //CmdSql2.Parameters.AddWithValue("@Nation", Nationd);
            CmdSql2.Parameters.AddWithValue("@Race", Race);
            CmdSql2.Parameters.AddWithValue("@Religion", Religion);
            CmdSql2.Parameters.AddWithValue("@LiveStatus", LiveStatus.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LiveHouseNumber", LiveHouseNumber);
            CmdSql2.Parameters.AddWithValue("@LiveHouseName", LiveHouseName);
            CmdSql2.Parameters.AddWithValue("@LiveHouseMoo", LiveHouseMoo);
            CmdSql2.Parameters.AddWithValue("@LiveHousesoy", LiveHousesoy);
            CmdSql2.Parameters.AddWithValue("@LiveStreetName", LiveStreetName);
            CmdSql2.Parameters.AddWithValue("@Livetambon", Livetambon);
            CmdSql2.Parameters.AddWithValue("@LiveCity", LiveCity);
            CmdSql2.Parameters.AddWithValue("@LiveProvince", LiveProvince);
            CmdSql2.Parameters.AddWithValue("@LivePostcode", LivePostcode);
            CmdSql2.Parameters.AddWithValue("@CensusHouseNumber", CensusHouseNumber);
            CmdSql2.Parameters.AddWithValue("@CensusHouseName", CensusHouseName);
            CmdSql2.Parameters.AddWithValue("@CensusHouseMoo", CensusHouseMoo);
            CmdSql2.Parameters.AddWithValue("@CensusHousesoy", CensusHousesoy);
            CmdSql2.Parameters.AddWithValue("@CensusStreetName", CensusStreetName);
            CmdSql2.Parameters.AddWithValue("@Censustambon", Censustambon);
            CmdSql2.Parameters.AddWithValue("@CensusCity", CensusCity);
            CmdSql2.Parameters.AddWithValue("@CensusProvince", CensusProvince);
            CmdSql2.Parameters.AddWithValue("@CensusPostcode", CensusPostcode);
            CmdSql2.Parameters.AddWithValue("@IncomePerMonth", IncomePerMonth);
            CmdSql2.Parameters.AddWithValue("@EduLevel", EduLevel.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@AddressOccupation", AddressOccupation);
            CmdSql2.Parameters.AddWithValue("@Occupation", Occupation.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@OherIncome", OherIncome);
            CmdSql2.Parameters.AddWithValue("@OherIncomeFrom", OherIncomeFrom);
            CmdSql2.ExecuteNonQuery();
            conn2.Close();

        }


















    }
}